
*****************************;
*** Diagenosis & Procedure ***;
*****************************;
%macro diag(startyr, endyr);
%do year = &startyr %to &endyr;
	%IF &year>2013 %then %let k= k;
	%IF &year<=2013 %then %let k= j;
	* MedPar - Inpatient & SNF;
	DATA diag_medpar; set cms.Medpar_all_file_&year; 
		if DGNS_1_CD="32723" or DGNS_1_CD="G4733" then OSA = 1;
		if DGNS_1_CD in ("32720","32721","32722","32723","32724","32725","32726","32727","32729","78057") or DGNS_1_CD in ("G4730","G4731","G4732","G4733","G4734","G4735","G4736","G4737","G4739") then SA = 1;
		if OSA=1 or SA=1;
		date = ADMSN_DT; diag_cd = DGNS_1_CD; type = 1; 
		keep BENE_ID date diag_cd type OSA SA;
	RUN;
	* Carrier;
	DATA diag_carrier; set cms.bcarrier_line_&k._&year; if NCH_CLM_TYPE_CD="71";
		if LINE_ICD_DGNS_CD="32723" or LINE_ICD_DGNS_CD="G4733" then OSA = 1;
		if LINE_ICD_DGNS_CD in ("32720","32721","32722","32723","32724","32725","32726","32727","32729","78057") or LINE_ICD_DGNS_CD in ("G4730","G4731","G4732","G4733","G4734","G4735","G4736","G4737","G4739") then SA = 1;
		if OSA=1 or SA=1;
		date = CLM_THRU_DT; diag_cd = LINE_ICD_DGNS_CD; type = 4;  
		keep BENE_ID date diag_cd type OSA SA;
	RUN;
	* Outpatient;
	DATA diag_outpatient; set cms.outpatient_base_claims_&k._&year; 
		if PRNCPAL_DGNS_CD="32723" or PRNCPAL_DGNS_CD="G4733" then OSA = 1;
		if PRNCPAL_DGNS_CD in ("32720","32721","32722","32723","32724","32725","32726","32727","32729","78057") or PRNCPAL_DGNS_CD in ("G4730","G4731","G4732","G4733","G4734","G4735","G4736","G4737","G4739") then SA = 1;
		if OSA=1 or SA=1;
		date = CLM_THRU_DT; diag_cd = PRNCPAL_DGNS_CD; type = 5; 
		keep BENE_ID date diag_cd type OSA SA;
	RUN;

	* Total;
	DATA temp; set diag_medpar diag_carrier diag_outpatient; RUN;
	%IF &year=&startyr %then %do;
		DATA diag_total; set temp; RUN;
	%END;
	%ELSE %do;
		PROC append base=diag_total data=temp force; RUN;
	%END;
%END;
%mend diag;
%diag(2009, 2015);
PROC datasets library=work; delete diag_medpar diag_carrier diag_outpatient temp; RUN;



************;
*** Cost ***;
************;
%macro cost(startyr, endyr);
%do year = &startyr %to &endyr;
	%IF &year>2013 %then %let k= k;
	%IF &year<=2013 %then %let k= j;
	* MedPar - Inpatient & SNF;
	DATA cost_medpar; set cms.Medpar_all_file_&year; 
		payment = MDCR_PMT_AMT+PASS_THRU_AMT; year = year(ADMSN_DT); qtr = qtr(ADMSN_DT); yq = YYQ(year,qtr); format yq yyq.; type = 1; 
		keep BENE_ID type yq payment;
	RUN;
	* Home Health Agency;
	DATA cost_hha; set cms.hha_base_claims_&k._&year; 
		payment = CLM_PMT_AMT; year = year(CLM_THRU_DT); qtr = qtr(CLM_THRU_DT); yq = YYQ(year,qtr); format yq yyq.; type = 2; 
		keep BENE_ID type yq payment;
	RUN;
	* Hospice;
	DATA cost_hospice; set cms.hospice_base_claims_&k._&year; 
		payment = CLM_PMT_AMT; year = year(CLM_THRU_DT); qtr = qtr(CLM_THRU_DT); yq = YYQ(year,qtr); format yq yyq.; type = 3; 
		keep BENE_ID type yq payment;
	RUN;
	* Carrier;
	DATA temp1; set cms.bcarrier_claims_&k._&year; 
		payment = CLM_PMT_AMT; year = year(CLM_THRU_DT); qtr = qtr(CLM_THRU_DT); yq = YYQ(year,qtr); format yq yyq.; type = 4;  
		keep BENE_ID yq payment NCH_CLM_TYPE_CD;
	RUN;
	DATA cost_carrier; set temp1; if NCH_CLM_TYPE_CD="71"; type = 41; RUN;
	DATA cost_carrier_dme; set temp1; if NCH_CLM_TYPE_CD="72"; type = 42; RUN;
	* Outpatient;
	DATA cost_outpatient; set cms.outpatient_base_claims_&k._&year; 
		payment = CLM_PMT_AMT; year = year(CLM_THRU_DT); qtr = qtr(CLM_THRU_DT); yq = YYQ(year,qtr); format yq yyq.; type = 5; 
		keep BENE_ID type yq payment;
	RUN;
	* DME carrier;
	DATA temp1; set cms.dme_line_&k._&year;
		payment = LINE_NCH_PMT_AMT; year = year(CLM_THRU_DT); qtr = qtr(CLM_THRU_DT); yq = YYQ(year,qtr); format yq yyq.; type = 6;
		keep BENE_ID yq payment NCH_CLM_TYPE_CD;
	RUN;
	DATA cost_dme_non; set temp1; if NCH_CLM_TYPE_CD="81"; type = 61; RUN;
	DATA cost_dme; set temp1; if NCH_CLM_TYPE_CD="82"; type = 62; RUN;
	* Part D;
	%IF &year>2011 %then %let file=file;
	%IF &year<=2011 %then %let file=saf_file;
	DATA cost_pde; set cms.pde_&file._&year; 
		payment = CVRD_D_PLAN_PD_AMT+NCVRD_PLAN_PD_AMT;
		year = year(SRVC_DT); qtr = qtr(SRVC_DT); yq = YYQ(year,qtr); format yq yyq.; type = 7; 
		keep BENE_ID type yq payment;
	RUN;

	* Total;
	DATA temp; set cost_medpar cost_hha cost_hospice cost_carrier cost_carrier_dme cost_outpatient cost_dme_non cost_dme cost_pde; RUN;
	%IF &year=&startyr %then %do;
		DATA cost_total; set temp; RUN;
	%END;
	%ELSE %do;
		PROC append base=cost_total data=temp force; RUN;
	%END;
%END;
%mend cost;
%cost(2009, 2015);
PROC datasets library=work; delete cost_medpar cost_hha cost_hospice cost_carrier cost_carrier_dme cost_outpatient cost_dme_non cost_dme cost_pde temp1 temp; RUN;

PROC SQL;
	CREATE table temp as
	SELECT BENE_ID, yq, type, sum(payment) as payment
	FROM cost_total
	GROUP by BENE_ID, yq, type;
QUIT;
DATA temp; set temp; 
	if type=1 or type=5 then payment_ipop = payment; 
	if type=42 or type=62 then payment_dme = payment; 
	if type=7 then payment_d = payment;
RUN;
PROC SQL;
	CREATE table cost_sum as
	SELECT BENE_ID, yq, sum(payment) as payment, sum(payment_ipop) as payment_ipop, sum(payment_dme) as payment_dme, sum(payment_d) as payment_d
	FROM temp
	GROUP by BENE_ID, yq;
QUIT;

DATA OSA_bene; set diag_total; keep BENE_ID; RUN;
PROC sort data=OSA_bene NODUPKEY; by BENE_ID; RUN;
PROC sort data=cost_sum; by BENE_ID; RUN;
DATA OSA_cost_sum; merge cost_sum (in = in1) OSA_bene (in = in2); by BENE_ID; if in2; RUN;


